package com.kk.permission.permissionlist;

import android.Manifest;
import android.provider.Settings;

import com.kk.permission.GuideConst;
import com.kk.permission.adapter.PermissionAdapter;
import com.kk.permission.checker.PermissionUtil;
import com.kk.permission.handler.ConfigHandler;
import com.kk.permission.utils.Permission;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PermissionListUtil {
    private static final int DEF_LIST_SIZE = 12;
    private static final Map<Permission, String> biMap = new HashMap(12);
    private static final Map<String, Permission> map = new HashMap(12);

    static {
        map.put(GuideConst.TOAST_PERMISSION, Permission.TOAST);
        map.put(GuideConst.CALL_RINGTONE_PERMISSION, Permission.CALL_RINGTONE);
        map.put(GuideConst.DIAL_NOTI_PERMISSION, Permission.DIAL_NOTI);
        map.put(GuideConst.AUTOBOOT_PERMISSION, Permission.AUTO_BOOT);
        map.put(GuideConst.SHOW_IN_LOCKSCREEN_PERMISSION, Permission.LOCK_SCREEN_SHOW);
        map.put(GuideConst.BACK_SHOW_PERMISSION, Permission.BACKGROUND_SHOW);
        map.put(GuideConst.BACKGROUND_PROTECT_PERMISSION, Permission.BACKGROUND_PROTECT);
        map.put(GuideConst.TRUST_APPLICATION_PERMISSION, Permission.TRUST_APP);
        map.put(GuideConst.DONT_OPTIMIZE_POWER, Permission.DONT_OPTIMIZE_POWER);
        map.put(GuideConst.INSTALL_SHORT_CUT, Permission.SHORTCUT);
        map.put(GuideConst.ALLOW_NOTI_PERMISSION, Permission.ALLOW_NOTI);
        map.put(GuideConst.SYSTEM_DIALING_PERMISSION, Permission.SYSTEM_DIALING);
        map.put(GuideConst.BACKGROUND_FROZEN_PERMISSION, Permission.BACKGROUND_FROZEN);
        for (String str : map.keySet()) {
            biMap.put(map.get(str), str);
        }
    }

    public static List<String> getAllOfPermissionNameList() {
        return new ArrayList(map.keySet());
    }

    public static List<String> getIntersectionPermissionNameList(List<String> list) {
        return getIntersectionList(getPermissionNameList(), list);
    }

    private static <R, T> List<R> getIntersectionList(List<R> list, List<T> list2) {
        list.retainAll(list2);
        return list;
    }

    public static List<Permission> getPermissionList() {
        return ConfigHandler.getInstance().getPermissionList();
    }

    public static List<String> getPermissionNameList() {
        List<String> arrayList = new ArrayList(12);
        for (Permission mapPermission2PermissionName : getPermissionList()) {
            arrayList.add(mapPermission2PermissionName(mapPermission2PermissionName));
        }
        return arrayList;
    }

    public static List<String> mapPermissionList2permissionNames(List<Permission> list) {
        List<String> arrayList = new ArrayList(12);
        for (Permission mapPermission2PermissionName : list) {
            arrayList.add(mapPermission2PermissionName(mapPermission2PermissionName));
        }
        return arrayList;
    }

    public static String mapPermission2PermissionName(Permission permission) {
        return (String) biMap.get(permission);
    }

//    public static List<IAccessibilityPermission> mapPermissionList2modelList(List<Permission> list) {
//        List<IAccessibilityPermission> arrayList = new ArrayList(12);
//        for (Permission mapPermission2model : list) {
//            arrayList.add(mapPermission2model(mapPermission2model));
//        }
//        return arrayList;
//    }

//    public static List<IAccessibilityPermission> mapPermissionNames2modelList(List<String> list) {
//        List<IAccessibilityPermission> arrayList = new ArrayList(12);
//        for (String str : list) {
//            arrayList.add(mapPermission2model((Permission) map.get(str)));
//        }
//        return arrayList;
//    }
//
//    public static IAccessibilityPermission mapPermissionName2model(String str) {
//        return mapPermission2model((Permission) map.get(str));
//    }

//    public static IAccessibilityPermission mapPermission2model(Permission permission) {
//        switch (permission) {
//            case TOAST:
//                return new FloatWindowPermission();
//            case CALL_RINGTONE:
//                return new RingTonePermission();
//            case DIAL_NOTI:
//                return new NotificationPermission();
//            case AUTO_BOOT:
//                return new AutobootPermission();
//            case LOCK_SCREEN_SHOW:
//                return new ShowInLockscreenPermission();
//            case BACKGROUND_SHOW:
//                return new BackShowPermission();
//            case BACKGROUND_PROTECT:
//                return new BackgroundProtectPermission();
//            case TRUST_APP:
//                return new TrustApplicationPermission();
//            case DONT_OPTIMIZE_POWER:
//                return new DontOptimizePowerPermission();
//            case SHORTCUT:
//                return new InstallShortCutPermission();
//            case ALLOW_NOTI:
//                return new AllowNotiPermission();
//            case SYSTEM_DIALING:
//                return new SystemDialingPermission();
//            case BACKGROUND_FROZEN:
//                return new BackgroundFrozenPermission();
//            default:
//                throw new RuntimeException("It is not supported permission!!");
//        }
//    }

    public static boolean isSupportModifyWriteSettingPermission(){
        return PermissionUtil.isPermissionGranted(Manifest.permission.WRITE_SETTINGS);
    }

    public static boolean notificationListenerEnable() {
        boolean enable = false;
        String flat = Settings.Secure.getString(PermissionAdapter.getAdapter().getAppContext().getContentResolver(), "enabled_notification_listeners");
        if (flat != null) {
            enable = flat.contains(GuideConst.PACKAGE_NAME);
        }
        return enable;
    }
}
